package com.jty.lib_base.net;

import android.text.TextUtils;

import com.blankj.utilcode.util.Utils;
import com.jty.lib_base.api.ApiService;
import com.jty.lib_base.net.cookie.CookieJarImpl;
import com.jty.lib_base.net.cookie.store.PersistentCookieStore;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * author: zhouyh
 * created on: 2021/5/12 11:51 上午
 * description:RetrofitClient封装单例类, 实现网络请求
 */
public class RetrofitClient {

    //超时
    private static final int DEFAULT_TIMEOUT = 20;

    private static Retrofit retrofit;

    private static class SingleInstance{
        private static final RetrofitClient instance = new RetrofitClient();
    }

    public static RetrofitClient getInstance(){
        return SingleInstance.instance;
    }

    private RetrofitClient() {
        this.RetrofitClient(ApiService.ApiUrl.URL_BASE,null);
    }

    private void RetrofitClient(String url, Map<String, String> headers) {
        if (TextUtils.isEmpty(url)){
            url = ApiService.ApiUrl.URL_BASE;
        }
        HttpLoggingInterceptor interceptor= new HttpLoggingInterceptor("retrofit");
        interceptor.setColorLevel(Level.SEVERE);
        interceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory();
        OkHttpClient okHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder())
                .cookieJar(new CookieJarImpl(new PersistentCookieStore(Utils.getApp().getApplicationContext())))
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .addInterceptor(new BaseInterceptor(headers))
                .addInterceptor(new RequestDataInterceptor())
                .addInterceptor(new ResponseInterceptor())
                .addInterceptor(interceptor)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                // 这里你可以根据自己的机型设置同时连接的个数和时间，我这里8个，和每个保持时间为10s
                .connectionPool(new ConnectionPool(15, 10, TimeUnit.SECONDS))
                .build();
        retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(url)
                .build();
    }


    /**
     * create you ApiService
     * Create an implementation of the API endpoints defined by the {@code service} interface.
     */
    public <T> T create(final Class<T> service){
        if (service==null){
            throw new RuntimeException("api service is null");
        }
        return retrofit.create(service);
    }

    /**
     * /**
     * execute your customer API
     * For example:
     * MyApiService service =
     * RetrofitClient.getInstance(MainActivity.this).create(MyApiService.class);
     * <p>
     * RetrofitClient.getInstance(MainActivity.this)
     * .execute(service.lgon("name", "password"), subscriber)
     * * @param subscriber
     */
    public static <T> T execute(Observable<T> observable, Observer<T> subscriber){
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return null;
    }
}
